package com.gnerv.battle.boot.project.guard.business.impl;

import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import com.gnerv.battle.boot.project.guard.business.MonitorProjectBusiness;
import com.gnerv.battle.boot.project.guard.model.bo.MonitorProjectBO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorProjectCreateDTO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorProjectDTO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorProjectUpdateDTO;
import com.gnerv.battle.boot.project.guard.service.MonitorProjectService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class MonitorProjectBusinessImpl implements MonitorProjectBusiness {

    private final MonitorProjectService monitorProjectService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(MonitorProjectCreateDTO monitorProjectCreateDTO) {
        MonitorProjectBO monitorProjectBO = monitorProjectCreateDTO.toMonitorProjectBO();
        monitorProjectBO.setUkId(UUIDTools.lower());
        monitorProjectService.create(monitorProjectBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        monitorProjectService.deleteByUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(MonitorProjectUpdateDTO monitorProjectUpdateDTO) {
        monitorProjectService.updateByUkId(monitorProjectUpdateDTO.toMonitorProjectBO());
        return RestResult.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<MonitorProjectDTO> selectByUkId(String ukId) {
        return RestResult.ok(monitorProjectService.selectByUkId(ukId));
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<MonitorProjectDTO>> listByName(String name, Integer page, Integer size) {
        return RestResult.ok(monitorProjectService.listByName(name, page, size));
    }

}
